package common.util;

import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import common.bean.RRException;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class FullTextUtil {


    public static Map<String, String> fetchPdfUrl(String pmId, Integer type) {
        String abase64 = XXTEA.encryptToBase64String(pmId + "", "R0HWlCI3ndd1s8S3");
        String url = StrUtil.format("https://www.pubtsg.com/api/getpdfurl.do?unitid=1&type={}&query={}", type, abase64);
        String html = HttpUtil.get(url);
        if (StrUtil.isNotBlank(html)) {
            JSONObject jsonObject = JSONUtil.parseObj(html);
            int flag = (int) jsonObject.get("flag");
            if (flag == 1) {
                String pdfPath = (String) jsonObject.get("pdfurl");
                Map<String, String> encryptMap = encryptPdf(pdfPath, 1, 1);
                String pdfUrl = encryptMap.get("pdfUrl");
                return encryptMap;
            }
        } else {
            throw new RRException("没有全文信息");
        }
        return null;

    }


    public static Map<String, String> encryptPdf(String pdfpath, Integer uid, Integer gid) {
        Map<String, String> map = new HashMap<>();
        if (StrUtil.isNotBlank(pdfpath)) {
            String[] prevEncryptStrArray = pdfpath.split(",");
            String encoderFileName = "";
            String encryptQuery = "";
            String preEncryptStr = "";
            //link是用来返回pc端申请全文时候的一个加密pdf地址。
            String links = "";
            for (String str : prevEncryptStrArray) {
//                if (!str.contains("|pdf")) {
//                    continue;
//                }
                String[] array = str.split("\\|");
                preEncryptStr = array[0] + "|" + array[1] + "|" + array[2];
                String fileName = "show";
                if (array.length > 3) {
                    //给str赋值去掉文件名的待加密字符串
                    str = str.replace("|" + array[3], "");
                    fileName = array[3].replace("#_#", ".");
                }
                String encryptStrPre = uid + "|" + gid + "|" + str + "|" + System.currentTimeMillis();
                encryptQuery = XXTEASafe.encryptToBase64String(encryptStrPre, "VJpHo2naZ9");
                String urlKeyStr = "pdfUrl";
                String suffix = ".pdf";
                if (str.contains("rar")) {
                    suffix = ".rar";
                    urlKeyStr = "rarUrl";
                }
                encoderFileName = URLEncoder.encode(fileName).replace("+", "%20");
                String newLink = "https://attach.pubtsg.com/attach/" + encoderFileName + "?query=" + encryptQuery + "&view=true&type=" + suffix;
                map.put(urlKeyStr, newLink);
            }
            if (links.length() > 1) {
                links = links.substring(1);
            }
            String keyMd5 = SecureUtil.md5(preEncryptStr);
            String keyHex = HexUtil.encodeHexStr(SecureUtil.md5(keyMd5 + "qf3.1415"));
//            log.info("当前查看的全文全文id:" + casesid + "--用户:" + uid + "--pdfpath:" + pdfpath + "---" + preEncryptStr + "---" + keyMd5 + "---" + keyHex);
            map.put("str", keyMd5);
            map.put("keyHex", keyHex);
            map.put("fileName", encoderFileName);
            map.put("query", encryptQuery);
            map.put("links", links);
        }
        return map;
    }

}
